import { _decorator, Sprite, Node, UITransform, tween, Tween } from 'cc';
import { EntityTypeEnum, StateTypeEnum } from '../../Common';
import { createUINode } from '../../Utils';
import { EntityManager } from '../../Base/EntityManager';
import { GunFireStateMachine } from './GunFireStateMachine';
import ResourcesManager from '../../Runtime/ResourcesManager';
import { MaterialEnum } from '../../Enum';
const { ccclass, property } = _decorator;

@ccclass('GunFireManager')
export class GunFireManager extends EntityManager {

    body: Node
    type: EntityTypeEnum
    tw: Tween<Node> = null
    init() {

        this.type = EntityTypeEnum.gunFire
        this.body = createUINode('body')
        this.body.parent = this.node

        const sprite = this.body.addComponent(Sprite)
        sprite.trim = false
        sprite.sizeMode = Sprite.SizeMode.CUSTOM
        sprite.customMaterial = ResourcesManager.Ins.materialMap.get(MaterialEnum.entityMaterial)
        this.body.getComponent(UITransform).setContentSize(16, 16)

        this.fsm = this.addComponent(GunFireStateMachine)
        this.fsm.init(this)

    }

    reset() {
        this.tw?.stop()
        this.tw = tween(this.node)
            .delay(0)
            .call(() => {
                this.state = StateTypeEnum.idle
            })
            .start()

        this.node.setPosition(2, 0)
    }
}